#include <iostream>
using std::cout;
using std::endl;


struct LinkNode
{
	LinkNode(int v = -1, LinkNode *n = NULL)
		: value(v), next(n) {}

	LinkNode *next;
	int value;
};


void print(LinkNode *head)
{
	while (head) {
		cout << head->value << endl;
		head = head->next;
	}
}


LinkNode* reverse(LinkNode *head)
{
	if (!head)
		return NULL;

	LinkNode *new_head = head;
	LinkNode *next = new_head->next;
	while (next) {
		LinkNode *tmp = next->next;
		next->next = new_head;
		new_head = next;
		next = tmp;
	}

	head->next = NULL;
	return new_head;
}


int main()
{
	// Construct 1->2->3->4
	LinkNode *head, *node;
	head = node = new LinkNode(1);
	int i = 2;
	while (i <= 4) {
		node->next = new LinkNode(i);
		node = node->next;
		++i;
	}
	print(head);

	// Reverse and print
	LinkNode *new_head = reverse(head);
	print(new_head);

	// Release
	node = new_head;
	while (node) {
		new_head = node->next;
		delete node;
		node = new_head;
	}

	return 0;
}
